import fs from 'fs';
import path from 'path';

function recursiver(folder_path, fn) {
  const dirents = fs.readdirSync(folder_path, { withFileTypes: true });
  for (let i = 0; i < dirents.length; i++) {
    const dirent = dirents[i];
    if (dirent.name.startsWith('.')) continue;
    if (dirent.isDirectory()) {
      recursiver(path.join(folder_path, dirent.name), fn);
    } else {
      fn(dirent.name, path.join(folder_path, dirent.name));
    }
  }
};

function getAllFiles(basepath) {
  const files = [];
  recursiver(basepath, (_, fullname) => {
    files.push(path.relative(basepath, fullname));
  });
  return files;
}

export { recursiver, getAllFiles };